package com.abing.starry.spi.quote;

import cn.hutool.core.io.resource.ResourceUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author CaptainBing
 * @Date 2024/9/19 15:50
 * @Description
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(FamousQuoteProperties.class)
public class FamousQuoteConfiguration {

    @Bean
    public FamousQuoteManager famousQuoteManager(FamousQuoteProperties famousQuoteProperties){
        FamousQuoteManager famousQuoteManager = new FamousQuoteManager();
        List<String> famousList = new ArrayList<>();
        famousQuoteProperties.getQuotes().forEach(path->{
            String famousStr = ResourceUtil.readUtf8Str(path);
            famousList.addAll(Arrays.stream(famousStr.split("\n"))
                                    .collect(Collectors.toList()));
        });
        famousList.forEach(famousQuoteManager::register);
        log.info("famousQuoteManager init success, famousList size:{}",famousList.size());
        return famousQuoteManager;
    }

}
